package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectInfo;
import com.mxpio.erp.common.plm.entity.ProjectProblem;
import com.mxpio.erp.common.plm.service.ProjectProblemService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectProblemController", description = "项目问题库")
@RestController
@RequestMapping("/erp/plm/pro/problem/")
public class ProjectProblemController {

    @Autowired
    private ProjectProblemService projectProblemService;

    @GetMapping("list")
    @Operation(summary = "任务问题列表", description = "任务问题列表（无分页）", method = "GET")
    public Result<List<ProjectProblem>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectProblem> list = projectProblemService.list(ProjectProblem.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "任务问题列表", description = "任务问题列表（分页）", method = "GET")
    public Result<Page<ProjectProblem>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectProblem> pageResult = projectProblemService.listPage(ProjectProblem.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{questionCode}")
    @Operation(summary = "根据问题编码获取问题列表", description = "根据问题编码获取问题列表", method = "GET")
    public Result<ProjectProblem> getById(@PathVariable(name = "questionCode", required = true) String questionCode) {
        ProjectProblem projectProblem = projectProblemService.getById(ProjectProblem.class, questionCode);
        return Result.OK(projectProblem);
    }

    @RequestMapping("add")
    @Operation(summary = "保存任务问题信息", description = "保存任务问题信息")
    public Result<ProjectProblem> add(@RequestBody ProjectProblem projectProblem) {
        projectProblemService.saveProblem(projectProblem);
        return Result.OK(projectProblem);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑任务问题信息", description = "编辑任务问题信息", method = "PUT")
    public Result<ProjectProblem> edit(@RequestBody ProjectProblem projectProblem) {
        projectProblemService.update(projectProblem);
        return Result.OK(projectProblem);
    }

    @PutMapping("deal")
    @Operation(summary = "处理任务问题信息", description = "处理任务问题信息", method = "PUT")
    public Result<ProjectProblem> deal(@RequestBody ProjectProblem projectProblem) {
        projectProblemService.dealProblem(projectProblem);
        return Result.OK(projectProblem);
    }

    @PutMapping("audit")
    @Operation(summary = "任务问题同意审核", description = "任务问题同意审核", method = "PUT")
    public Result<ProjectProblem> audit(@RequestBody ProjectProblem projectProblem) {
        projectProblemService.problemAudit(projectProblem);
        return Result.OK(projectProblem);
    }

    @PutMapping("audit/no")
    @Operation(summary = "任务问题不同意审核", description = "任务问题不同意审核", method = "PUT")
    public Result<ProjectProblem> auditNo(@RequestBody ProjectProblem projectProblem) {
        projectProblemService.problemAuditNo(projectProblem);
        return Result.OK(projectProblem);
    }



    @DeleteMapping(value = "delete/{questionCode}")
    @Operation(summary = "删除任务问题信息",description = "删除任务问题信息", method = "DELETE")
    public Result<?> delete(@PathVariable(name = "questionCode") String questionCode) {
        projectProblemService.delete(questionCode,ProjectProblem.class);
        return Result.OK("删除成功");
    }

    @GetMapping("page/head")
    @Operation(summary = "我负责的问题列表", description = "我负责的问题列表（分页）", method = "GET")
    public Result<Page<ProjectProblem>> myHead(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectProblem> pageResult = projectProblemService.listPageHead(page,criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("page/audit")
    @Operation(summary = "我审核的问题列表", description = "我审核的问题列表（分页）", method = "GET")
    public Result<Page<ProjectProblem>> myAudit(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectProblem> pageResult = projectProblemService.listPageAudit(page,criteria);
        return Result.OK(pageResult);
    }


}
